package Logic.utils;


import org.junit.Test;

/**
 * @author liujun
 * @version V1.0
 * @date 2022/2/27 4:56 PM
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @description 读文件 处理函数
 * 字符串转二维矩阵： TransString2Matrix
 * 字符串转一维数组： TransString2IntArray
 */
public class StringHandler {

    public static String[][] TransString2StringMatrix(String params) {
        return null;
    }

    // int[][] 需要处理的是 {{}} 内的数据 ,{{1,2},{3,4}} ==> [[1,2],[3,4]}
    public static int[][] TransString2IntMatrix(String params) {
        // null 的处理
        if (params == null || "".equals(params) || " ".equals(params) || params.contains("= null;")) {
            return null;
        }
        // 空的处理
        if (params.indexOf("{") + 1 == params.indexOf("}")) {
            return new int[0][];
        }
        // 正常处理
        int begin = 0;
        String[] curStrArray = params.split("}, \\{");
        if (curStrArray.length <= 1) {
            return null;
        }
        int row = curStrArray.length;
        int column = curStrArray[1].split(",").length;

        int[][] paramsMatrix = new int[row][column];

        // 对每个 { 进行处理
        int subBegin = begin;
        int count = 0;
        for (int i = begin; i < params.length(); i++) {
            char c = params.charAt(i);
            // 对每个 { 进行处理
            if (c == '{') {
                subBegin = i + 1;
            }
            // 对每个 } 进行处理
            if (c == '}' && count < row && subBegin < params.length()) {
                paramsMatrix[count] = TransString2IntArray(params.substring(subBegin, i));
                count++;
            }
        }
        return paramsMatrix;
    }

    public static String TransString2FormatString(String params) {
        if (params == null) {
            return null;
        }
        params = params.replaceAll("\"", "");
        if ("empty".equals(params)) {
            return "";
        }
        if ("null".equals(params)) {
            return null;
        }
        return params.trim();
    }

    public static String[] TransString2StringArray(String params) {
        String[] paramsStrArray = preCheck(params);
        if (paramsStrArray == null) {
            return null;
        }
        String[] paramsIntArray = new String[paramsStrArray.length];
        for (int i = 0; i < paramsStrArray.length; i++) {
            switch (paramsStrArray[i].trim()) {
                case "U":
                default:
                    paramsIntArray[i] = paramsStrArray[i].trim();
            }
        }
        return paramsIntArray;
    }

    public static int TransString2Int(String params) {
        return Integer.parseInt(params.trim());
    }

    public static int[] TransString2IntArray(String params) {
        String[] paramsStrArray = preCheck(params);
        if (paramsStrArray == null) {
            return null;
        }
        int[] paramsStringArray = new int[paramsStrArray.length];
        for (int i = 0; i < paramsStrArray.length; i++) {
            switch (paramsStrArray[i].trim()) {
                case "U":
                    paramsStringArray[i] = Integer.MAX_VALUE;
                default:
                    paramsStringArray[i] = Integer.parseInt(paramsStrArray[i].trim());
            }
        }
        return paramsStringArray;
    }

    // {1,2,3} ==> [1,2,3]
    public static String[] preCheck(String params) {
        params = params.replaceAll("\\{", "").replaceAll("}", "");
        params = params.replaceAll("\\[", "").replaceAll("]", "");
        params = params.replaceAll("\"", "");

        // 是否仅含 【大小写字母、数字、负数、逗号、空格】
        String pattern = "[-,a-zA-Z\\s\\d*]+";
        if (!params.matches(pattern)) {
            System.out.println("Error, params :" + params);
            return null;
        }
        return params.split(",");
    }


    @Test
    public void test() {
        String s = "a1, b1, c1, d1, e1, f1, g1, h1, i1, j1";
        if (preCheck(s) == null) {
            System.out.println("error");
        }
    }

    public void test1() {
        String s1 = "int[][] demo01 = {{4, 4}, {6, 1}, {7, 1}, {5, 0}, {7, 0}, {5, 2}};";
        String s = "a1, b1, c1, d1, e1, f1, g1, h1, i1, j1";
        int[][] list = TransString2IntMatrix(s);
        ArrayUtilsImpl.intMatrixPrint(list);
    }
}
